import sys
sys.path.append('C:/Users/Hu/Dropbox/Research/PythonWork/Cancer/src/STAT/')

import numpy as np
import csv
from datetime import datetime
from time import strftime
import matplotlib.pyplot as plt
import math
import random
from scipy import stats
import ols
from scipy.stats import f


def getCurTime():
    """
    get current time
    Return value of the date string format(%Y-%m-%d %H:%M:%S)
    """
    format='%Y-%m-%d %H:%M:%S'
    sdate = None
    cdate = datetime.now()
    try:
        sdate = cdate.strftime(format)
    except:
        raise ValueError
    return sdate

def build_data_list(inputCSV):
    sKey = []
    fn = inputCSV
    f = open(inputCSV)
    #ra = csv.DictReader(file(fn), dialect="excel")
    ra = csv.DictReader(f, dialect="excel")
    
    for record in ra:
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        for item in ra.fieldnames:
            temp = int(float(record[item]))
            sKey.append(temp)
    sKey = np.array(sKey)
    sKey.shape=(-1,len(ra.fieldnames))
    return sKey

def fivenum(v):
    """Returns Tukey's five number summary (minimum, lower-hinge, median, upper-hinge, maximum) for the input vector, a list or array of numbers based on 1.5 times the interquartile distance"""
    import numpy as np
    from scipy.stats import scoreatpercentile
    try:
        np.sum(v)
    except TypeError:
        print('Error: you must provide a list or array of only numbers')
    q1 = scoreatpercentile(v,25)
    q3 = scoreatpercentile(v,75)
    md = np.median(v)
    return [np.min(v), q1, md, q3, np.max(v)]

#--------------------------------------------------------------------------
#MAIN

if __name__ == "__main__":
    print '===================================================='
    print "begin at " + getCurTime()

    filepath = 'C:/_DATA/migration_census_2000/REDCAPLUS_25Region/'
    file = 'C:/_DATA/migration_census_2000/REDCAPLUS_25Region/25reg_regressiondata.csv'
    file = 'C:/_DATA/migration_census_2000/REDCAPLUS_100Region/100reg_regressiondata.csv'
    file = 'C:/_DATA/migration_census_2000/STATE/census_migration_state.csv'
    file = 'C:/_DATA/migration_census_2000/census_county_migration_format.csv'

    #pop file
    file = 'C:/_DATA/migration_census_2000/REDCAPLUS_25Region/census_migration_25regs_pop.csv'
    #file = 'C:/_DATA/migration_census_2000/REDCAPLUS_100Region/census_migration_100regs_pop.csv'
    #file = 'C:/_DATA/migration_census_2000/STATE/census_migration_state_pop2000.csv'
    #file = 'C:/_DATA/migration_census_2000/census_county_pop2000.csv'

    #inoutflow file
    file = 'C:/_DATA/migration_census_2000/REDCAPLUS_25Region/migration_25regs_inoutflow.csv'
    #file = 'C:/_DATA/migration_census_2000/REDCAPLUS_100Region/census_migration_100regs_inoutflow.csv'
    #file = 'C:/_DATA/migration_census_2000/STATE/census_migration_state_inoutflow.csv'
    #file = 'C:/_DATA/migration_census_2000/census_county_inoutflow.csv'
    
    data = build_data_list(file)    #[dist,grossout,grossin,vol]

    # inflow, outflow, pop [min, q1, median, q3, max, mean]
    # flow [min, q1, median, q3, max, mean, sum, count]

    summarydata = data[:,1]
    temp = fivenum(summarydata)
    temp.append(np.mean(summarydata))
    temp.append(np.sum(summarydata))
    temp.append(len(summarydata))
    #print temp
    print [int(round(val)) for val in temp]

    headerstr = 'exp'
    fileLoc = filepath + 'temp.csv'
    #np.savetxt(fileLoc, yhat, delimiter=',', header = headerstr, fmt = '%s')
